package org.example.fileswalk;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FilesWalkExample1 {
    public static void main(String[] args) {
        Path path = Paths.get("F:\\迅雷下载");
        /*List<Path> pathList = listFiles(path);
        System.err.println(JSONObject.toJSONString(pathList,true));
        List<Path> pathList1 = listFiles(path,1);
        System.err.println(JSONObject.toJSONString(pathList1,true));*/
        List<Path> directories = listDirectories(path);
        System.err.println(JSONObject.toJSONString(directories,true));
    }

    static List<Path> listFiles(Path path){
        List<Path> result = null;
        try (Stream<Path> pathStream = Files.walk(path)) {
            result = pathStream.filter(Files::isRegularFile)
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    static List<Path> listFiles(Path path,int maxDepth){
        List<Path> result = null;
        try (Stream<Path> pathStream = Files.walk(path, maxDepth)) {
            result = pathStream.filter(Files::isRegularFile).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    static List<Path> listDirectories(Path path){
        List<Path> result = null;
        try (Stream<Path> pathStream = Files.walk(path)) {
            result = pathStream.filter(Files::isDirectory).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
